package com.chenshu.shop.service;

import com.chenshu.shop.common.Constant;
import com.chenshu.shop.common.SecurityUtil;
import com.chenshu.shop.mapper.UserMapper;
import com.chenshu.shop.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-06
 * Time：08:46
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    UserMapper userMapper;
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<User> updateUser(Integer id, String  password) {
        Integer row = userMapper.updateUser(id, SecurityUtil.encrypt(password));
        if (row == 1) {
            log.info("修改用户密码成功");
        } else {
            log.info("修改用户密码失败");
        }
        return getAllUsers();
    }

    public User getUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public List<User> updateUserInfo(User user) {
        Integer row = userMapper.updateUserInfo(user);
        if (row == 1) {
            log.info("更新用户信息成功");
        } else {
            log.info("更新用户信息失败");
        }
        return getAllUsers();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<User> deleteUser(Integer id) {
        Integer row = userMapper.deleteUser(id);
        if (row == 1) {
            log.info("删除用户成功");
        } else {
            log.info("删除用户失败");
        }
        return getAllUsers();
    }

    @Transactional(rollbackFor = Exception.class)
    public List<User> saveUser(User user) {
        //将密码设置为加密后的
        user.setPassword(SecurityUtil.encrypt(user.getPassword()));
        Integer row = userMapper.addUser(user);
        if (row == 1) {
            log.info("新增用户成功");
        } else {
            log.info("新增用户失败");
        }
        return getAllUsers();
    }

    public void register(User user) {
        user.setPassword(SecurityUtil.encrypt(user.getPassword()));
        Integer row = userMapper.addUser(user);
        if (row == 1) {
            log.info("用户注册成功");
        } else {
            log.info("用户注册失败");
        }
    }

    public boolean login(String username, String password, HttpServletRequest request) {
        //用于判断账号密码是否正确
        boolean flag = false;
        User userInfo = userMapper.selectUserByUsername(username);
        if (userInfo != null) {
            flag = SecurityUtil.decrypt(password, userInfo.getPassword());
        }
        //如果密码正确，就将管理员信息存入Session
        if (flag) {
            HttpSession session = request.getSession(true);
            session.setAttribute(Constant.SESSION_USER_INFO_KEY, userInfo);
            session.setAttribute("msg", "登录成功");
        }
        return flag;
    }

    public Boolean checkUsername(String username) {
        User user = userMapper.selectUserByUsername(username);
        if (user != null) {
            log.info(user.toString());
        }
        return user == null;
    }
}
